﻿using System.Net;
using System.Net.Sockets;
using System.Threading;
using System;

namespace DotNet.Common
{
    public class UDPCommunicateClient
    {
        string _host;
        public IPAddress IP
        {
            set { _host = value.ToString(); }
        }

        public string Host
        {
            set { _host = value; }
        }

        ushort _port;
        public ushort Port
        {
            get { return _port; }
            set { _port = value; }
        }

        int _timeDelay;
        public int TimeDelay
        {
            get { return _timeDelay; }
            set { _timeDelay = value; }
        }

        public event VoidInvoker SendOnce;
        public event VoidInvoker SendBegin;
        public event VoidInvoker SendEnd;

        public UDPCommunicateClient()
        {

        }

        public UDPCommunicateClient(string host,ushort port)
        {
            _host = host;
            _port = port;
        }

        public UDPCommunicateClient(IPAddress ip, ushort port)
        {
            _host = ip.ToString();
            _port = port;
        }

        UdpClient _client;
        public bool Start(string desIP,ushort desPort)
        {
            try
            {
                _client = new UdpClient(_host,_port);
                _client.Connect(desIP, desPort);

                if (SendBegin != null)
                    SendBegin();

                return true;
            }
            catch
            {
                Stop();
                return false;
            }
        }

        public bool Stop()
        {
            if (_client != null)
            {
                try
                {
                    _client.Close();
                    _client = null;
                }
                catch
                {
                    return false;
                }
                finally
                {
                    if (SendEnd != null)
                        SendEnd();
                }
            }

            return true;
        }

        public bool Send(byte[][] dataArray)
        {
            if (_client == null)
                return false;

            if (dataArray == null)
                return false;

            foreach (byte[] data in dataArray)
            {
                if (data == null)
                    continue;

                _client.Send(data, data.Length);

                if (SendOnce != null)
                    SendOnce();

                Thread.Sleep(_timeDelay);
            }

            return true;
        }

        public bool Send(byte[] dataArray)
        {
            if (_client == null)
                return false;

            if (dataArray == null)
                return false;

            _client.Send(dataArray, dataArray.Length);

            if (SendOnce != null)
                SendOnce();

            return true;
        }
    }
}
